package threading;

import java.util.concurrent.PriorityBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:定时器的代码实现
 * User: xwl
 * Date: 2023-08-10
 * Time: 22:06
 */
// 表示一个任务
class MyTask implements  Comparable<MyTask>{
    public Runnable runnable;
    // 为了方便后续判定,使用绝对的时间戳
    public long time;

    // 构造方法
    public MyTask(Runnable runnable, long delay) {
        this.runnable = runnable;
        // 取当前时刻的时间戳 + delay , 作为该任务实际执行的时间戳
        this.time = System.currentTimeMillis() + delay;
    }

    @Override
    public int compareTo(MyTask o) {
        // 这样写意味着每次取出的是时间最小的元素
        return (int)(this.time - o.time);
    }
}

class MyTimer {
    // 带有优先级的阻塞队列(核心数据结构)
    private PriorityBlockingQueue<MyTask> queue = new PriorityBlockingQueue<>();

    // 创建一个锁对象
    public Object locker = new Object();

    public void schedule(Runnable runnable, long delay) {
        // 根据参数,构造 MyTask, 插入队列
        MyTask myTask = new MyTask(runnable,delay);
        queue.put(myTask);
        synchronized (locker) {
            locker.notify();
        }
    }

    // 构造线程,负责执行具体任务
    public MyTimer() {
        Thread t = new Thread( () -> {
            while (true) {
                try {
                    synchronized (locker) {
                        MyTask myTask = queue.take();
                        long curTime = System.currentTimeMillis();
                        if (myTask.time <= curTime) {
                            // 时间倒了,可以执行任务了
                            myTask.runnable.run();
                        } else {
                            // 时间还没到,把刚才取出的任务重新塞回队列
                            queue.put(myTask);
                            locker.wait(myTask.time - curTime);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t.start();
    }
}


public class ThreadDemo20 {
    public static void main(String[] args) {
//        System.out.println(System.currentTimeMillis());
        MyTimer myTimer = new MyTimer();
        myTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello4");
            }
        }, 4000);
        myTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello3");
            }
        }, 3000);
        myTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello2");
            }
        }, 2000);
        myTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello1");
            }
        }, 1000);

        System.out.println("hello");
    }
}
